package features.advance.jdk.concurrent;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author LIN
 * @date 2023-02-08 10:55
 */
public class CompletableFutureDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        TestCase.test_in();

    }

    public static class TestCase{


        public static void test_in(){
            Supplier supplier = new Supplier<List<String>>() {
                @Override
                public List<String> get() {
                    return null;
                }
            };
            BiConsumer<List<String>, Integer> biConsumer = new BiConsumer<List<String>, Integer>() {

                @Override
                public void accept(List<String> strings, Integer integer) {

                }


            };

            new Function(){

                @Override
                public Object apply(Object o) {
                    return null;
                }
            };




        }



        public static void test_new_CompletableFuture_01(){
            CompletableFuture<Object> objectCompletableFuture = new CompletableFuture<>();
        }
        public static void test_new_CompletableFuture_02(){
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName()+"  run");
                }
            };
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(runnable);

        }

        public static void test_new_CompletableFuture_03() throws ExecutionException, InterruptedException {

            Supplier<String> uSupplier = new Supplier<String>() {
                @Override
                public String get() {
                    return "test_new_CompletableFuture_03";
                }
            };
            CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(uSupplier);
            String result = completableFuture.get();
            System.out.println(result);
        }

        /**
         * // 沿用上一个任务的线程池
         * public <U> CompletableFuture<U> thenApply(
         *     Function<? super T,? extends U> fn) {
         *     return uniApplyStage(null, fn);
         * }
         *
         * //使用默认的 ForkJoinPool 线程池（不推荐）
         * public <U> CompletableFuture<U> thenApplyAsync(
         *     Function<? super T,? extends U> fn) {
         *     return uniApplyStage(defaultExecutor(), fn);
         * }
         * // 使用自定义线程池(推荐)
         * public <U> CompletableFuture<U> thenApplyAsync(
         *     Function<? super T,? extends U> fn, Executor executor) {
         *     return uniApplyStage(screenExecutor(executor), fn);
         * @throws ExecutionException
         * @throws InterruptedException
         */
        public static void test_thenApply() throws ExecutionException, InterruptedException {
            CompletableFuture<String> future = CompletableFuture.completedFuture("hello!").thenApply(s -> s + "world!");
            System.out.println(future.get());
            // 这次调用将被忽略
            future.thenApply(s->s+"nice");
            System.out.println(future.get());
        }
        public static void test_thenApply_thenApplly() throws ExecutionException, InterruptedException {
            CompletableFuture<String> future = CompletableFuture.completedFuture("hello!").thenApply(s -> s + "world!").thenApply(s->s+"nice");
            System.out.println(future.get());
        }


        /**
         * 那 thenCompose() 和 thenCombine() 有什么区别呢？
         *
         * thenCompose() 可以两个 CompletableFuture 对象，并将前一个任务的返回结果作为下一个任务的参数，它们之间存在着先后顺序。
         * thenCombine() 会在两个任务都执行完成后，把两个任务的结果合并。两个任务是并行执行的，它们之间并没有先后依赖顺序。
         */
        public static void test_thenCombine(){

        }

        /**
         * allOf() 方法会等到所有的 CompletableFuture 都运行完成之后再返回
         */
        public static void test_allOf(){

            CompletableFuture<Void> task1 =
                    CompletableFuture.supplyAsync(new Supplier<Void>() {
                        @Override
                        public Void get() {
                            System.out.println("task1");
                            return null;
                        }
                    });
            CompletableFuture<Void> task2 =
                    CompletableFuture.supplyAsync(new Supplier<Void>() {
                        @Override
                        public Void get() {
                            System.out.println("task2");
                            return null;
                        }
                    });
            CompletableFuture<Void> task3 =
                    CompletableFuture.supplyAsync(new Supplier<Void>() {
                        @Override
                        public Void get() {
                            System.out.println("task3");
                            return null;
                        }
                    });
            CompletableFuture<Void> task4 =
                    CompletableFuture.supplyAsync(new Supplier<Void>() {
                        @Override
                        public Void get() {
                            System.out.println("task4");
                            return null;
                        }
                    });
            CompletableFuture<Void> task5 =
                    CompletableFuture.supplyAsync(new Supplier<Void>() {
                        @Override
                        public Void get() {
                            System.out.println("task5");
                            return null;
                        }
                    });
            CompletableFuture<Void> task6 =
                    CompletableFuture.supplyAsync(()->{
                        //自定义业务操作
                        System.out.println("task6");
                        return null;
                    });
            CompletableFuture<Void> headerFuture=CompletableFuture.allOf(task1,task2,task3,task4,task5,task6);

            try {
                headerFuture.join();
            } catch (Exception ex) {
            }
            System.out.println("all done. ");
        }


        /**
         * anyOf() 方法不会等待所有的 CompletableFuture 都运行完成之后再返回，只要有一个执行完成即可！
         */
        public static void test_anyOf(){

        }








            public static void  test_label(){
            int count=2;
            tryComplete: if(true){
                System.out.println("test");
                if(count>1){
                    break tryComplete;
                }
                System.out.println("2");
            }

        }
    }



}
